//
//  SignatureViewTool.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/19.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "SignatureViewTool.h"
#import "WZSwitch.h"
#import "UIViewController+CBPopup.h"
@interface SignatureListCell : XHBaseTableCell

@end

@implementation SignatureListCell
- (void)setupUI{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.textLabel.font = kDefaultFont(13);
    self.detailTextLabel.font = kDefaultFont(13);
    self.textLabel.textColor = UIColorHex(#666666);
    self.detailTextLabel.textColor = UIColorHex(#666666);
}
@end


@interface SignatureSubViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UILabel*titleLab;
@property (nonatomic,strong)UIButton*cancelBtn;
@property (nonatomic,strong)UIButton*sureBtn;

@property (nonatomic,strong)UILabel*accountLab;
@property (nonatomic,strong)WZSwitch*switchView;
@property (nonatomic,strong)UILabel*subTitleLab;
@property (nonatomic,strong)UITableView*contentView;
@property (nonatomic,strong)UIView*whiteContentView;
@property (nonatomic,strong)UIView*freeContentView;
@property (nonatomic,strong)UISwitch*freeSwitch;
@property (nonatomic,assign)BOOL beJson;
///加入白名单
@property (nonatomic,assign)BOOL beWhite;
///免弹窗
@property (nonatomic,assign)BOOL beFree;
@property (nonatomic,assign)BOOL didWhite;
@property (nonatomic,assign)BOOL showJson;

@property (nonatomic,copy) void(^BeWhiteBlock)(BOOL beWhite);
@property (nonatomic,strong) SignatureModel *model;
@property (nonatomic,strong) NSMutableDictionary *whiteDic;
@property (nonatomic,strong) NSArray *allKeys;
@property (nonatomic,strong) NSMutableArray *chooseArr;
@property (nonatomic,strong) DappInfo *dappInfo;
@property (nonatomic,strong) WhiteDappInfo *whiteDappInfo;
@property (nonatomic,copy) void(^cancelBlock)(void);
@property (nonatomic,copy) void(^sureBlock)(void);
@property (nonatomic,strong) UIScrollView*jsonSroll;
@property (nonatomic,strong) UILabel*jsonLab;
@property (nonatomic,strong) UIView*jsonBackView;
@end
@implementation SignatureSubViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    [self setupUI];
}
- (void)setShowJson:(BOOL)showJson{
    _showJson = showJson;
    self.jsonSroll.hidden = !showJson;
}
- (void)setModel:(SignatureModel *)model{
    _model = model;
    self.allKeys = model.jsonData.allKeys;
    ///保存data 准备白名单操作
    self.whiteDic = [NSMutableDictionary dictionary];
    NSDictionary *authorizationDic = [model.authorization objectOrNilAtIndex:0];
    if (authorizationDic && ValidDict(authorizationDic)) {
        self.accountLab.text = model.permissions;
    }
    
    self.whiteContentView.hidden = model.isWhite;
//    self.beWhite = model.isWhite;
    [self.whiteContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(model.isWhite?0.01f:AdaptedWidth(120));
    }];
    if (model.isWhite) {
        if (model.whiteDappInfo) {
            self.whiteDappInfo = model.whiteDappInfo;
        }
        self.didWhite = model.isWhite;
        self.freeContentView.alpha = 1;
        UIImageView*imageV = [self.freeContentView viewWithTag:10001];
        imageV.image = IMAGE_NAMED(@"sigh");
        self.freeSwitch.enabled = YES;
    }
    
    self.jsonLab.text = model.jsonStr;
    [self.contentView reloadData];
}
- (void)setBeWhite:(BOOL)beWhite{
    _beWhite = beWhite;
    self.freeContentView.alpha = beWhite?1:.5;
    UIImageView*imageV = [self.freeContentView viewWithTag:10001];
    imageV.image = beWhite?IMAGE_NAMED(@"sigh"):IMAGE_NAMED(@"gray_sigh");
    if (!beWhite) {
        self.freeSwitch.on = NO;
        self.beFree = NO;
    }
    self.freeSwitch.enabled = self.beWhite;
    if (beWhite) {
        for (int i = 0; i< self.allKeys.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            //全选实现方法
            [self.contentView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
            [self.chooseArr addObject:indexPath];
            ///默认白名单的值为任意
            [self.whiteDic setObject:@"" forKey:self.allKeys[i]];
        }
    }else{
        [self.whiteDic removeAllObjects];
        [self.chooseArr removeAllObjects];
    }
}
- (void)setupUI{
    kWeakSelf(self);

    self.view.backgroundColor = UIColor.whiteColor;
    ViewRadius(self.view, AdaptedWidth(13));
    self.titleLab = [UILabel new];
    self.titleLab.textColor = UIColorHex(333333);
    self.titleLab.font = kBoldFont(17);
    self.titleLab.text = XLocalized(@"签名请求");
    [self.view addSubview:_titleLab];

    self.accountLab = [UILabel new];
    self.accountLab.textColor = UIColorHex(#FFCC03);
    self.accountLab.font = kDefaultFont(13);
    [self.view addSubview:self.accountLab];
    self.switchView = [WZSwitch new];
    self.switchView.leftString = XLocalized(@"属性");
    self.switchView.rightString = XLocalized(@"Json");
    [self.switchView setSelectColor:UIColorHex(#FFFFFF)];
    [self.switchView setUnselectColor:UIColorHex(333333)];
    [self.switchView setMoveViewColor:UIColorHex(#FFCC03)];
    [self.switchView setTextFont:kBoldFont(12)];
    [self.switchView setBackgroundColor:UIColorHex(#F7F7F7)];
    self.switchView.block = ^(BOOL state) {
        weakself.showJson = state;
    };
    [self.view addSubview:self.switchView];
    self.subTitleLab = [UILabel new];
    self.subTitleLab.font = kBoldFont(13);
    self.subTitleLab.text = XLocalized(@"合约 → 操作");
    [self.view addSubview:self.subTitleLab];
    self.contentView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.contentView.delegate =self;
    self.contentView.dataSource =self;
    self.contentView.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.contentView.backgroundColor = UIColorHex(#F7F7F7);
    self.contentView.rowHeight = AdaptedWidth(30);
    ViewRadius(self.contentView, AdaptedWidth(7));
    [self.view addSubview:self.contentView];
    
    self.jsonSroll = [UIScrollView new];
    self.jsonSroll.backgroundColor = UIColorHex(#F7F7F7);
    
    [self.view addSubview:self.jsonSroll];
    self.jsonLab = [UILabel new];
    self.jsonLab.textColor = UIColorHex(333333);
    self.jsonLab.font = kDefaultFont(15);
    self.jsonLab.numberOfLines = 0;
    [self.jsonSroll addSubview:self.jsonLab];
    
    self.whiteContentView = [UIView new];
    [self.view addSubview:self.whiteContentView];
    UILabel*whiteTitleLab = [UILabel new];
    whiteTitleLab.font =kBoldFont(15);
    whiteTitleLab.textColor = UIColorHex(333333);
    whiteTitleLab.text = XLocalized(@"开启白名单");
    [self.whiteContentView addSubview:whiteTitleLab];
    [whiteTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(AdaptedWidth(7));
    }];
    UISwitch *whiteSwitch = [UISwitch new];
    [self.whiteContentView addSubview:whiteSwitch];
    [whiteSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(whiteTitleLab.mas_centerY);
        make.right.mas_equalTo(0);
    }];
    whiteSwitch.onTintColor = kColorMain;
    [whiteSwitch addTarget:self action:@selector(whiteSwitchClick:) forControlEvents:UIControlEventValueChanged];

    UILabel*whiteContentLab = [UILabel new];
    whiteContentLab.adjustsFontSizeToFitWidth = YES;
    whiteContentLab.font = SYSTEMFONT(12);
    whiteContentLab.numberOfLines = 0;
    whiteContentLab.text = XLocalized(@"您可以将此操作列入白名单，下次就不必再次手动授权。勾选属性旁边的单选按钮意味着您允许您可以允许此事务的某些属性的更改，并且仅当其他属性发生变化才不会被列入白名单。");
    [self.whiteContentView addSubview:whiteContentLab];
    [whiteContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(whiteSwitch.mas_bottom).offset(AdaptedWidth(3));
        make.bottom.left.right.mas_equalTo(0);
    }];
    
    self.freeContentView = [UIView new];
    [self.view addSubview:self.freeContentView];
    UILabel*freeTitleLab = [UILabel new];
    freeTitleLab.font =kBoldFont(15);
    freeTitleLab.textColor = UIColorHex(333333);
    freeTitleLab.text = XLocalized(@"免弹窗");
    [self.freeContentView addSubview:freeTitleLab];
    [freeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(AdaptedWidth(7));
    }];
    UISwitch *freeSwitch = [UISwitch new];
    self.freeSwitch = freeSwitch;
    [self.freeContentView addSubview:freeSwitch];
    [freeSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(freeTitleLab.mas_centerY);
        make.right.mas_equalTo(0);
    }];
    freeSwitch.onTintColor = kColorMain;
    [freeSwitch addTarget:self action:@selector(freeSwitchClick:) forControlEvents:UIControlEventValueChanged];
    UIView *bottomV = [UIView new];
    bottomV.backgroundColor = UIColorHex(#F7F7F7);
    bottomV.layer.cornerRadius = AdaptedWidth(7);
    [self.freeContentView addSubview:bottomV];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(freeSwitch.mas_bottom).offset(AdaptedWidth(3));
    }];
    
    UIImageView*sigh = [UIImageView new];
    sigh.image = IMAGE_NAMED(@"sigh");
    sigh.tag = 10001;
    [bottomV addSubview:sigh];
    [sigh mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(20), AdaptedWidth(20)));
    }];
    UILabel*freeContentLab = [UILabel new];
    freeContentLab.textColor = UIColorHex(333333);
    freeContentLab.font = SYSTEMFONT(12);
    freeContentLab.numberOfLines = 0;
    [bottomV addSubview:freeContentLab];
    [freeContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sigh.mas_right).offset(kNormalSpace);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-kNormalSpace);
    }];
    freeContentLab.text = XLocalized(@"打开免弹窗，下次进行该操作时，无需进行弹窗确认直接执行，开启后可前往更多中关闭");
    self.cancelBtn = [UIButton new];
    [self.cancelBtn setTitle:XLocalized(@"取消") forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
    [self.cancelBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself cb_dismissPopupViewControllerAnimated:YES completion:^{
            !weakself.cancelBlock?:weakself.cancelBlock();
        }];
    }];
//    ViewBorderRadius(self.cancelBtn, 0, CGFloatToPixel(.5), UIColorHex(#C7C7CC));
    [self.view addSubview:self.cancelBtn];
    self.sureBtn = [UIButton new];
    [self.sureBtn setTitle:XLocalized(@"确定") forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:UIColorHex(#FFCC03) forState:UIControlStateNormal];
    [self.sureBtn addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.sureBlock?:weakself.sureBlock();
    }];
//    ViewBorderRadius(self.sureBtn, 0, CGFloatToPixel(.5), UIColorHex(#C7C7CC));
    [self.view addSubview:self.sureBtn];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(25));
        make.centerX.mas_equalTo(0);
    }];
    [self.accountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(AdaptedWidth(25));
        make.left.mas_equalTo(kNormalSpace);
    }];
    [self.switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.accountLab.mas_centerY);
        make.right.mas_equalTo(-kNormalSpace);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(90), AdaptedWidth(30)));
    }];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountLab.mas_left);
        make.top.equalTo(self.accountLab.mas_bottom).offset(AdaptedWidth(10));
    }];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(130));
        make.top.equalTo(self.subTitleLab.mas_bottom).offset(AdaptedWidth(10));
    }];
    [self.jsonSroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
  
    [self.jsonLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.left.right.top.equalTo(self.jsonSroll);
    }];
    [self.whiteContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_bottom).offset(AdaptedWidth(20));
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(AdaptedWidth(120));
    }];
    [self.freeContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.whiteContentView.mas_bottom);
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(AdaptedWidth(100));
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.right.equalTo(self.sureBtn.mas_left);
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(0);
        make.height.width.equalTo(self.cancelBtn);
    }];
    
    UIView*line = [UIView new];
    line.backgroundColor = UIColorHex(#C7C7CC);
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.sureBtn.mas_top);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(CGFloatFromPixel(1));
    }];
    UIView*line2 = [UIView new];
    line2.backgroundColor = UIColorHex(#C7C7CC);
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom);
        make.centerX.bottom.mas_equalTo(0);
        make.width.mas_equalTo(CGFloatFromPixel(1));
    }];
    self.beWhite = NO;
    self.showJson = NO;
}
- (void)whiteSwitchClick:(UISwitch*)whiteSwitch{
    self.beWhite = whiteSwitch.on;
    self.freeSwitch.enabled = whiteSwitch.on;
//    !self.BeWhiteBlock?:self.BeWhiteBlock(self.beWhite);
    if (self.beWhite) {
        self.whiteDappInfo = [WhiteDappInfo new];
        self.whiteDappInfo.free = self.beFree;
        self.whiteDappInfo.whites = self.whiteDic.copy;
        self.whiteDappInfo.contract_name = self.model.account;
        self.whiteDappInfo.name = self.model.name;
        self.whiteDappInfo.permissions = self.model.permissions;
    }else{
        self.whiteDappInfo = nil;
    }

    [self.contentView setEditing:self.beWhite animated:YES];
}
- (void)freeSwitchClick:(UISwitch*)freeSwitch{
    self.beFree = freeSwitch.on;
    ///已经在白名单里  添加免弹窗
    if (self.whiteDappInfo) {
        self.whiteDappInfo.free = self.beFree;
    }
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allKeys.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SignatureListCell*cell = [SignatureListCell cellForTableView:tableView andCellStyle:UITableViewCellStyleValue1];
    NSString*key=[self.allKeys objectOrNilAtIndex:indexPath.row];
        cell.textLabel.text = key;
    if ([self.model.jsonData[key] isKindOfClass:NSString.class]) {
        cell.detailTextLabel.text = self.model.jsonData[key];
    }
    cell.selected = [self.chooseArr containsObject:indexPath];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return !self.model?0.01f:AdaptedWidth(30.f);
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel*titleLab =[UILabel new];
    titleLab.backgroundColor = self.contentView.backgroundColor;
    titleLab.font = kBoldFont(15);
    titleLab.textColor = UIColorHex(333333);
    titleLab.text= !self.model?@"":[NSString stringWithFormat:@" %@ → %@",self.model.account,self.model.name];;
    return titleLab;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.chooseArr containsObject:indexPath]) {
        [self.chooseArr removeObject:indexPath];
        ///取消该属性的白名单  记录值
        [self.whiteDic setObject:self.model.jsonData[self.allKeys[indexPath.row]] forKey:self.allKeys[indexPath.row]];
        self.whiteDappInfo.whites = self.whiteDic.copy;
    }else{
        [self.chooseArr addObject:indexPath];
        [self.whiteDic setObject:@"" forKey:self.allKeys[indexPath.row]];
        self.whiteDappInfo.whites = self.whiteDic.copy;
    }
    [tableView reloadData];
}
- (NSMutableArray*)chooseArr{
    if (!_chooseArr) {
        _chooseArr =[NSMutableArray array];
    }
    return _chooseArr;
}
- (void)dealloc{
    DLog(@"dealloc");
}
@end

@interface SignatureViewTool()
@property (nonatomic,weak)SignatureSubViewController*signatureVc;
@end

@implementation SignatureViewTool
SINGLETON_FOR_CLASS(SignatureViewTool)
- (void)alertController:(UIViewController*)controller withSignatureModel:(SignatureModel*)signatureModel DappInfo:(DappInfo*)dappInfo cancelAction:(void(^)(void))cancelAction sureAction:(void(^)(WhiteDappInfo*whiteDappInfo,BOOL didWhite))sureAction{
    if (self.signatureVc) {
        return;
    }
    SignatureSubViewController *vc = [SignatureSubViewController new];
    self.signatureVc = vc;
    vc.view.frame = CGRectMake(0, 0, AdaptedWidth(330), signatureModel.isWhite?AdaptedWidth(430):AdaptedWidth(550));
    [controller cb_presentPopupViewController:vc animationType:CBPopupViewAnimationFade aligment:CBPopupViewAligmentCenter overlayDismissEnabled:NO
                     overlayDismissed:nil];
    vc.model = signatureModel;
    vc.dappInfo = dappInfo;
    
    vc.cancelBlock = ^{
        !cancelAction?:cancelAction();
    };
    kWeakSelf(vc);
    vc.sureBlock = ^{
        DLog(@"whiteDic\n  %@",weakvc.whiteDic);
        ///是否开启了白名单
//        WhiteDappInfo*whiteDappInfo;
//        if (weakvc.beWhite) {
//            whiteDappInfo = [WhiteDappInfo new];
//            whiteDappInfo.free = weakvc.beFree;
//            whiteDappInfo.whites = weakvc.whiteDic.copy;
//            whiteDappInfo.contract_name = signatureModel.account;
//            whiteDappInfo.name = signatureModel.name;
//        }else{
//            whiteDappInfo = nil;
//        }
        ///当选择了白名单  之后选了n免d弹窗
//        if (weakvc.beFree) {
//        if (weakvc.whiteDappInfo) {
//            whiteDappInfo = weakvc.whiteDappInfo;
//        }
//        }
        !sureAction?:sureAction(weakvc.whiteDappInfo,weakvc.didWhite);
    };
}
- (void)dissmiss{
    if (self.signatureVc) {
        [self.signatureVc cb_dismissPopupViewControllerAnimated:NO completion:nil];
        self.signatureVc = nil;
    }
   
}

- (void)checkWhites:(Wallet*)curWallet{
    DLog(@"curWallet \n %@",curWallet.whiteDappJson);
}
////查看签名是否在白名单里
- (void)checkSignatureModel:(SignatureModel*)signatureModel withWallet:(Wallet*)wallet dapp:(DappInfo*)dappInfo callBack:(void(^)(BOOL isWhite,BOOL isFree,WhiteDappInfo *whiteDappInfo))callBack{
    DLog(@"curWallet \n %@",wallet.whiteDappJson);

   __block BOOL isWhite = NO;
   __block BOOL isFree = NO;
   __block NSArray*whites;
    [wallet.whiteDapps enumerateObjectsUsingBlock:^(DappInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DLog(@"obj.dappId  %@ permissions %@",obj.dappId,obj.permissions);
        if ([obj.dappId isEqualToString:dappInfo.dappId] && [obj.permissions isEqualToString:signatureModel.permissions]) {
            whites = obj.whites.copy;
        }
    }];
    if (!whites || !whites.count) {
        !callBack?:callBack(NO,NO,nil);
        return;
    }
    ///记录在白名单里的签名
    __block WhiteDappInfo*whiteDappInfo;
    [whites enumerateObjectsUsingBlock:^(WhiteDappInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ///判断合约名和操作有没有在白名单里
        DLog(@"permissions  == %@",obj.permissions);
        if ([obj.contract_name isEqualToString:signatureModel.account] && [obj.name isEqualToString:signatureModel.name]&&[obj.whites.allKeys isEqualToArray:signatureModel.jsonData.allKeys]&&[signatureModel.permissions isEqualToString:obj.permissions]) {
            NSString*whitesStr = [obj.whites.allValues componentsJoinedByString:@""];
            if (!whitesStr.length) {
                ///所有属性值都任意
                whiteDappInfo = obj;
                isWhite = YES;
                isFree = obj.free;
                *stop = YES;
            }else{
                if (isWhite) {
                    *stop = YES;
                    return;
                }
                [obj.whites enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSString* value, BOOL * _Nonnull stop) {
                    DLog(@"%@ %@",key,value);
                    if (value.length) {
                        NSString*signV = [NSString stringWithFormat:@"%@",signatureModel.jsonData[key]];
                        if (![value isEqualToString:signV]) {
                            isWhite = NO;
                            isFree = NO;
                            whiteDappInfo = nil;
                            *stop = YES;
                        }else{
                            ///如果有在白名单记录，给用户 设置免弹窗使用
                            whiteDappInfo = obj;
                            isWhite = YES;
                            isFree = obj.free;
                        }
                    }
                }];
            }
        }
    }];
    !callBack?:callBack(isWhite,isFree,whiteDappInfo);
}

- (void)addWhiteDappInfo:(WhiteDappInfo*)whiteDappInfo withWallet:(Wallet*)wallet dapp:(DappInfo*)dappInfo{
   
    NSMutableArray*walletWhits = wallet.whiteDapps.mutableCopy;
    __block DappInfo*curDappInfo;
    [walletWhits enumerateObjectsUsingBlock:^(DappInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.dappId isEqualToString:dappInfo.dappId] && [whiteDappInfo.permissions isEqualToString:obj.permissions]) {
            curDappInfo = obj;
        }
    }];
    NSMutableArray*whites = curDappInfo.whites.mutableCopy;
    if (!whites) {
        whites = [NSMutableArray array];
    }
    if (whites.count) {
       __block BOOL isHave=NO;
        [whites enumerateObjectsUsingBlock:^(WhiteDappInfo* obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.whites isEqualToDictionary:whiteDappInfo.whites] && [obj.contract_name isEqualToString:whiteDappInfo.contract_name] && [obj.name isEqualToString:whiteDappInfo.name]&&[whiteDappInfo.permissions isEqualToString:obj.permissions]) {
                [whites replaceObjectAtIndex:idx withObject:whiteDappInfo];
                *stop = YES;
                isHave = YES;
            }
        }];
        if (!isHave) {
            [whites addObject:whiteDappInfo];
        }
    }else{
        [whites addObject:whiteDappInfo];
    }
    if (!curDappInfo) {
        curDappInfo = dappInfo;
        curDappInfo.permissions = whiteDappInfo.permissions;
        curDappInfo.whites =whites.copy;
        [walletWhits addObject:curDappInfo];
        wallet.whiteDappJson = [walletWhits modelToJSONString];
        [[XHDataBaseManager shareInstance]updateWalletToDB:wallet];
    }else{
        curDappInfo.whites =whites.copy;
        [walletWhits replaceObjectAtIndex:[walletWhits indexOfObject:curDappInfo] withObject:curDappInfo];
        wallet.whiteDappJson = [walletWhits modelToJSONString];
        [[XHDataBaseManager shareInstance]updateWalletToDB:wallet];
    }
}
- (NSMutableDictionary*)curWalletPwdDic{
    if (!_curWalletPwdDic) {
        _curWalletPwdDic = [NSMutableDictionary dictionary];
    }
    return _curWalletPwdDic;
}
@end
